package com.aube.interact.fighter.card;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by huyaonan on 16/11/24.
 */
public class RevealAnimateView extends View {

    private Paint mPaint;
    private Paint mRipplePaint;

    private int solidColor;
    private int rippleColor;

    private int ALPHA = 102;

    private float counter = 0;

    private boolean mScaleIn = true;
    private float mRadiusRate = 0f;

    public RevealAnimateView(Context context) {
        this(context, null);
    }

    public RevealAnimateView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RevealAnimateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        solidColor = Color.argb(ALPHA, 255, 255, 255);
        rippleColor = Color.argb(ALPHA, 255, 255, 255);

        mPaint = new Paint();
        mPaint.setColor(solidColor);
        mPaint.setAntiAlias(true);

        mRipplePaint = new Paint();
        mRipplePaint.setColor(rippleColor);
        mRipplePaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();

        canvas.drawCircle(width/2, height/2, width/4*mRadiusRate/10, mPaint);

        float size = counter%(width/4) + width/4;
        mRipplePaint.setAlpha((int) ((1-size*1f/(width/2))*ALPHA));
        canvas.drawCircle(width/2, height/2, size*mRadiusRate/10, mRipplePaint);
        counter+=0.75;

        if(mRadiusRate <= 10 && mRadiusRate >= 0) {
            if (mScaleIn)
                mRadiusRate+=1;
            else
                mRadiusRate-=1;
            if(mRadiusRate > 10) mRadiusRate = 10;
            if(mRadiusRate < 0) mRadiusRate = 0;
        }

        if(mRadiusRate == 0 && !mScaleIn && getVisibility() == View.VISIBLE)
            super.setVisibility(GONE);

        invalidate();
    }

}
